package com.hitrust.gs.net;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.Map;
import java.util.Iterator;

import org.apache.log4j.Logger;

import com.hitrust.gs.lib.GSLogWriter;

import com.hitrust.gs.integration.GSApepInfoProcess;


public class HttpServiceServlet extends HttpServlet {
	
	public static final String SUCCESS = "1";
    
	public static final String FAIL = "0";
	
	public static final String TESTING_OK = "TestOK";
	
	public static final String TESTING_COMMAND = "TestJZCG";
	
	public void init(ServletConfig aConfig) throws ServletException 
	{
		
	}
	
	
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger("socket");
	
		
	public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException 	
	{
		
		try {
			String tParam = request.getQueryString();
			tParam = HttpConnection.urlDecode(tParam);
			PrintWriter out = response.getWriter();
			out.print(excute(tParam));
			
		} catch (Exception e) {
			GSLogWriter.error(logger,e.getLocalizedMessage());
		}
		
	}
	
	public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException 	
	{
		try {
			StringBuffer buffer = new StringBuffer("");
			request.setCharacterEncoding("UTF-8");

			//String action = request.getParameter("Action");

			Map map = request.getParameterMap();
			Iterator iterator = map.entrySet().iterator();
			boolean isFirst = true;
			while (iterator.hasNext()) {


				Map.Entry tTemp = (Map.Entry) iterator.next();
				
				String key = HttpConnection.urlDecode((String) tTemp.getKey());
				if("END".equalsIgnoreCase(key))
					continue;

				if (!isFirst)
					buffer.append("&");
				
				
				Object oValue = tTemp.getValue();
				String value;
				if (oValue instanceof String[]) {
					value = ((String[]) oValue)[0];
				} else {
					value = oValue.toString();
				}

				value = HttpConnection.urlDecode(value);

				buffer.append(key).append("=").append(value);

				if (isFirst) {
					isFirst = false;
				}
			}
			buffer.append("&end");
			PrintWriter out = response.getWriter();
			out.write(excute(buffer.toString()));
			
		} catch (Exception e) {
			GSLogWriter.error(logger,e.getLocalizedMessage(),"dopost执行失败");
			//e.printStackTrace();
		}		
	}	
	
	public String excute(String instr) throws Exception
	{
			
		if ((instr != null) && (instr.toUpperCase().indexOf(TESTING_COMMAND.toUpperCase()) != -1)) {
			//GSLogWriter.error(logger, this, "excute执行失败"); 
//		///	SocketUtil.send(iClient, TESTING_OK, "utf-8", "");
			return TESTING_OK;
		}
		else {
			if ((instr != null) && (instr.length() > 0)) { 
				GSLogWriter.info(logger,this, "client input is :" + instr);

				int idx = instr.toUpperCase().indexOf("&END");                
				if (idx > -1){
					String rt = "";
					GSApepInfoProcess gsip = new GSApepInfoProcess();
					rt = gsip.process(instr.substring(0, idx ));  
			
					return rt;
				} else {
					return FAIL;
				}
//                instr = in.readLine();
//		///		instr = SocketUtil.receive(iClient, "GB2312", "");
			} else {
				return FAIL;
			}
		}
	}
	

}
